function Snake(ele) {
    //保存地图
    this.ele = ele
    // 蛇
    this.len = 20
    this.Snakeobj = [
        { x: 3, y: 2, color: 'white', ele: null },
        { x: 2, y: 2, color: 'skyblue', ele: null },
        { x: 1, y: 2, color: 'skyblue', ele: null }
    ]
    // 定义初始方向
    this.direction = 'right'
}

Snake.prototype.remove = function () {
    for (let i = 0; i < this.Snakeobj.length; i++) {
        if (this.Snakeobj[i].ele) {
            this.ele.removeChild(this.Snakeobj[i].ele)
        }
    }
}



Snake.prototype.create = function () {

    this.remove()
    for (let i = 0; i < this.Snakeobj.length; i++) {
        let divobj = document.createElement('div')
        divobj.style.width = this.len + 'px'
        divobj.style.height = this.len + 'px'
        divobj.style.position = 'absolute'
        divobj.style.left = this.Snakeobj[i].x * this.len + 'px'
        divobj.style.top = this.Snakeobj[i].y * this.len + 'px'
        divobj.style.background = this.Snakeobj[i].color
        this.Snakeobj[i].ele = divobj

        this.ele.appendChild(divobj)
    }
}
Snake.prototype.move = function () {
    // 蛇向右移动
    // for (let i = 0; i < this.Snakeobj.length; i++) {
    //     this.Snakeobj[i].x +=  1
    // }

    for (let i = this.Snakeobj.length - 1; i > 0; i--) {
        this.Snakeobj[i].x = this.Snakeobj[i - 1].x
        this.Snakeobj[i].y = this.Snakeobj[i - 1].y
    }
    switch (this.direction) {
        case 'top':
            this.Snakeobj[0].y -= 1
            break;
        case 'bottom':
            this.Snakeobj[0].y += 1
            break;
        case 'left':

            this.Snakeobj[0].x -= 1
            break;
        case 'right':

            this.Snakeobj[0].x += 1
            break;
    }
    this.create()
}
Snake.prototype.die = function () {
    let snakeXMax = this.ele.clientWidth / this.len - 1
    let snakeYMax = this.ele.clientHeight / this.len - 1
    if (this.Snakeobj[0].x < 0 || this.Snakeobj[0].x > 39) {
        console.log('你有瑕疵啊凸皿凸');
    }

}